package model.diagram.part;

import model.ModelPackage;
import model.Workspace;
import model.diagram.edit.parts.ActorEditPart;
import model.diagram.edit.parts.ActorNameEditPart;
import model.diagram.edit.parts.ActorPackActorEditPart;
import model.diagram.edit.parts.ActorsPackageEditPart;
import model.diagram.edit.parts.AssociationActorToSerieEditPart;
import model.diagram.edit.parts.EpisodeEditPart;
import model.diagram.edit.parts.EpisodeNameEditPart;
import model.diagram.edit.parts.EpisodesPackEpisodeEditPart;
import model.diagram.edit.parts.EpisodesPackageEditPart;
import model.diagram.edit.parts.RoleEditPart;
import model.diagram.edit.parts.RoleNameEditPart;
import model.diagram.edit.parts.SeasonEditPart;
import model.diagram.edit.parts.SeasonNameEditPart;
import model.diagram.edit.parts.SeasonPackageEditPart;
import model.diagram.edit.parts.SeasonsPackSeasonEditPart;
import model.diagram.edit.parts.SerieEditPart;
import model.diagram.edit.parts.SerieNameEditPart;
import model.diagram.edit.parts.SerieRoleEditPart;
import model.diagram.edit.parts.SeriesPackSerieEditPart;
import model.diagram.edit.parts.SeriesPackageEditPart;
import model.diagram.edit.parts.WorkspaceEditPart;

import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.View;

/**
 * This registry is used to determine which type of visual object should be
 * created for the corresponding Diagram, Node, ChildNode or Link represented
 * by a domain model object.
 * 
 * @generated
 */
public class ModelVisualIDRegistry {

	/**
	 * @generated
	 */
	private static final String DEBUG_KEY = "org.ProjectWithoutName.diagram/debug/visualID"; //$NON-NLS-1$

	/**
	 * @generated
	 */
	public static int getVisualID(View view) {
		if (view instanceof Diagram) {
			if (WorkspaceEditPart.MODEL_ID.equals(view.getType())) {
				return WorkspaceEditPart.VISUAL_ID;
			} else {
				return -1;
			}
		}
		return model.diagram.part.ModelVisualIDRegistry.getVisualID(view
				.getType());
	}

	/**
	 * @generated
	 */
	public static String getModelID(View view) {
		View diagram = view.getDiagram();
		while (view != diagram) {
			EAnnotation annotation = view.getEAnnotation("Shortcut"); //$NON-NLS-1$
			if (annotation != null) {
				return (String) annotation.getDetails().get("modelID"); //$NON-NLS-1$
			}
			view = (View) view.eContainer();
		}
		return diagram != null ? diagram.getType() : null;
	}

	/**
	 * @generated
	 */
	public static int getVisualID(String type) {
		try {
			return Integer.parseInt(type);
		} catch (NumberFormatException e) {
			if (Boolean.TRUE.toString().equalsIgnoreCase(
					Platform.getDebugOption(DEBUG_KEY))) {
				ModelDiagramEditorPlugin.getInstance().logError(
						"Unable to parse view type as a visualID number: "
								+ type);
			}
		}
		return -1;
	}

	/**
	 * @generated
	 */
	public static String getType(int visualID) {
		return Integer.toString(visualID);
	}

	/**
	 * @generated
	 */
	public static int getDiagramVisualID(EObject domainElement) {
		if (domainElement == null) {
			return -1;
		}
		if (ModelPackage.eINSTANCE.getWorkspace().isSuperTypeOf(
				domainElement.eClass())
				&& isDiagram((Workspace) domainElement)) {
			return WorkspaceEditPart.VISUAL_ID;
		}
		return -1;
	}

	/**
	 * @generated
	 */
	public static int getNodeVisualID(View containerView, EObject domainElement) {
		if (domainElement == null) {
			return -1;
		}
		String containerModelID = model.diagram.part.ModelVisualIDRegistry
				.getModelID(containerView);
		if (!WorkspaceEditPart.MODEL_ID.equals(containerModelID)) {
			return -1;
		}
		int containerVisualID;
		if (WorkspaceEditPart.MODEL_ID.equals(containerModelID)) {
			containerVisualID = model.diagram.part.ModelVisualIDRegistry
					.getVisualID(containerView);
		} else {
			if (containerView instanceof Diagram) {
				containerVisualID = WorkspaceEditPart.VISUAL_ID;
			} else {
				return -1;
			}
		}
		switch (containerVisualID) {
		case WorkspaceEditPart.VISUAL_ID:
			if (ModelPackage.eINSTANCE.getActorsPackage().isSuperTypeOf(
					domainElement.eClass())) {
				return ActorsPackageEditPart.VISUAL_ID;
			}
			if (ModelPackage.eINSTANCE.getSeriesPackage().isSuperTypeOf(
					domainElement.eClass())) {
				return SeriesPackageEditPart.VISUAL_ID;
			}
			if (ModelPackage.eINSTANCE.getSeasonPackage().isSuperTypeOf(
					domainElement.eClass())) {
				return SeasonPackageEditPart.VISUAL_ID;
			}
			if (ModelPackage.eINSTANCE.getEpisodesPackage().isSuperTypeOf(
					domainElement.eClass())) {
				return EpisodesPackageEditPart.VISUAL_ID;
			}
			if (ModelPackage.eINSTANCE.getActor().isSuperTypeOf(
					domainElement.eClass())) {
				return ActorEditPart.VISUAL_ID;
			}
			if (ModelPackage.eINSTANCE.getSerie().isSuperTypeOf(
					domainElement.eClass())) {
				return SerieEditPart.VISUAL_ID;
			}
			if (ModelPackage.eINSTANCE.getSeason().isSuperTypeOf(
					domainElement.eClass())) {
				return SeasonEditPart.VISUAL_ID;
			}
			if (ModelPackage.eINSTANCE.getEpisode().isSuperTypeOf(
					domainElement.eClass())) {
				return EpisodeEditPart.VISUAL_ID;
			}
			if (ModelPackage.eINSTANCE.getRole().isSuperTypeOf(
					domainElement.eClass())) {
				return RoleEditPart.VISUAL_ID;
			}
			break;
		}
		return -1;
	}

	/**
	 * @generated
	 */
	public static boolean canCreateNode(View containerView, int nodeVisualID) {
		String containerModelID = model.diagram.part.ModelVisualIDRegistry
				.getModelID(containerView);
		if (!WorkspaceEditPart.MODEL_ID.equals(containerModelID)) {
			return false;
		}
		int containerVisualID;
		if (WorkspaceEditPart.MODEL_ID.equals(containerModelID)) {
			containerVisualID = model.diagram.part.ModelVisualIDRegistry
					.getVisualID(containerView);
		} else {
			if (containerView instanceof Diagram) {
				containerVisualID = WorkspaceEditPart.VISUAL_ID;
			} else {
				return false;
			}
		}
		switch (containerVisualID) {
		case WorkspaceEditPart.VISUAL_ID:
			if (ActorsPackageEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (SeriesPackageEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (SeasonPackageEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (EpisodesPackageEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (ActorEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (SerieEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (SeasonEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (EpisodeEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (RoleEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case ActorEditPart.VISUAL_ID:
			if (ActorNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case SerieEditPart.VISUAL_ID:
			if (SerieNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case SeasonEditPart.VISUAL_ID:
			if (SeasonNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case EpisodeEditPart.VISUAL_ID:
			if (EpisodeNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case RoleEditPart.VISUAL_ID:
			if (RoleNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		}
		return false;
	}

	/**
	 * @generated
	 */
	public static int getLinkWithClassVisualID(EObject domainElement) {
		if (domainElement == null) {
			return -1;
		}
		if (ModelPackage.eINSTANCE.getSeriesPackSerie().isSuperTypeOf(
				domainElement.eClass())) {
			return SeriesPackSerieEditPart.VISUAL_ID;
		}
		if (ModelPackage.eINSTANCE.getActorPackActor().isSuperTypeOf(
				domainElement.eClass())) {
			return ActorPackActorEditPart.VISUAL_ID;
		}
		if (ModelPackage.eINSTANCE.getSeasonsPackSeason().isSuperTypeOf(
				domainElement.eClass())) {
			return SeasonsPackSeasonEditPart.VISUAL_ID;
		}
		if (ModelPackage.eINSTANCE.getEpisodesPackEpisode().isSuperTypeOf(
				domainElement.eClass())) {
			return EpisodesPackEpisodeEditPart.VISUAL_ID;
		}
		if (ModelPackage.eINSTANCE.getSerieRole().isSuperTypeOf(
				domainElement.eClass())) {
			return SerieRoleEditPart.VISUAL_ID;
		}
		if (ModelPackage.eINSTANCE.getAssociationActorToSerie().isSuperTypeOf(
				domainElement.eClass())) {
			return AssociationActorToSerieEditPart.VISUAL_ID;
		}
		return -1;
	}

	/**
	 * User can change implementation of this method to handle some specific
	 * situations not covered by default logic.
	 * 
	 * @generated
	 */
	private static boolean isDiagram(Workspace element) {
		return true;
	}

}
